import Taro from '@tarojs/taro'
import { concatUrl } from '@/utils'
import { getAssistanceOrder } from '@/api/rescue'

const app = Taro.getApp()
const pages = app.config.pages

// 命令与路径对照表
// const linkMap = {
//     // 商品列表
//     'goodsList': 'pages/goods/list/index',
//     // 商品详情
//     'goods': 'pages/goods/details/index',
//     // 年卡
//     'annual': 'pages/annual/index/index',
//     // 我的车库
//     'car': 'pages/car/list/index',
//     // 卡券列表
//     'coupon': 'pages/coupon/index/index',
//     // 车辆检测
//     'detection': 'pages/detection/record/index',
//     // 门店列表
//     'store': 'pages/store/index/index',
//     // 首页
//     'index': 'pages/tabbar/index/index',
//     // 购物车
//     'cart': 'pages/tabbar/cart/index',
//     // 分类
//     'category': 'pages/tabbar/category/index',
//     // 我的(个人中心)
//     'user': 'pages/tabbar/user/index',
//     // 保养助手
//     'maintain': 'pages/maintain/list/index',
//     // 专题
//     'topic': 'pages/webview/h5/index',
//     // 直播
//     'live': 'pages/live/index/index',
//     // 年卡订单确认
//     'annual-order': 'pages/order/index/index',
// }




// 判断是否属于TabBar
const isTabBar = (link: string): boolean => pages.includes(link)

// export const changeToLink = (command: ICommand) => {
//     if (!command) return ''
//     if (!command.commandName) return ''
//     let route = '/' + linkMap[command.commandName]
//     const tabBar = isTabBar(linkMap[command.commandName])
//     if (command.query) {
//         route = concatUrl(route, command.query)
//     }
//     return {
//         isTabBar: tabBar,
//         url: route
//     }
// }

// obj 详情参数
export default function (obj) {
  console.log('obj: ', obj);
  // contentType 内容类型（0-商品，1-年卡，2-专题，3-小程序，4-H5,5-前端服务项目）
  const { contentType, thumbnail, serviceItems, isDistribution, appId } = obj
  // goods 为首页基础服务项目商品列表 接口数据 格式不一样
  if (thumbnail) {
    Taro.navigateTo({ url: concatUrl('/pages/goods/details/index', { id: obj.id }) })
  }
  // 热门必推 分销商品走这里
  if (isDistribution) {
    Taro.navigateTo({ url: '/pages/goods/distributionList/index' })
    return
  }
  if (contentType == 0) {
    Taro.navigateTo({ url: concatUrl('/pages/goods/details/index', { id: serviceItems }) })
  }
  if (contentType == 1) {
    Taro.navigateTo({ url: '/pages/annual/index/index' })
  }
  if (contentType == 2) {
    Taro.navigateTo({ url: concatUrl('/pages/webview/h5/index', { id: serviceItems, path: '/topic/common' }) })
  }
  if (contentType == 3) {
    // const serviceItems = '/pages/goods/list/index?id=27'
    // if (serviceItems == '/pages/rescue/index/index') {
    //     return getAssistanceOrder().then(async res => {
    //         const { data: { data } } = res
    //         if (Array.isArray(data) && data.length == 0) {
    //             Taro.navigateTo({ url: '/pages/rescue/index/index' })
    //         } else {
    //             Taro.navigateTo({ url: '/pages/rescue/detail/index' })
    //         }
    //     })
    // }
    const tabBar = isTabBar(serviceItems.substr(1))
    if (tabBar) {
      Taro.switchTab({ url: serviceItems })
    } else {
      Taro.navigateTo({ url: serviceItems })
    }
  }
  if (contentType == 4) {
    // const serviceItems = 'https://test-fn-m.jekunauto.com/#/annual-review/annual-card?read=true&storeName=广州艺景路店&vendorName=广州友邻&carLicense=浙YHFQQFK&mobile=13202936085&shop=[{"goodsId":1,"relatedNum":"51","goodsName":"精致洗车","annualCardPrice":"100.00","platformPrice":"100.00","thumbnailUrl":"https://test-pub-api.jekunauto.com/uploads/6/20210305/i3Ktb5Vlh1Vg6S9nSzHn8JxhR4QXtvxe.jpg","eachUse":2,"buyNum":5,"type":0,"tags":[]}]&validDay=100&total=100.00'
    // const serviceItems = 'http://localhost:8080/#/insurance/driving'
    // 车险跳转这里做专门配置
    // const linkUrl = ['https://fn-m.jekunauto.com/', 'https://fn-m.jekunauto.com/#/insurance/index', 'https://test-fn-m.jekunauto.com/#/insurance/index', 'https://test-fn-m.jekunauto.com/', 'https://test-fn-m.jekunauto.com/#/insurance/driving']
    // if (linkUrl.includes(serviceItems)) {
    // return Taro.navigateTo({ url: concatUrl('/pages/webview/index/index', { path: 'https://test-fn-m.jekunauto.com/#/topic/common', rest: "" }) })
    // }
    if (serviceItems.includes('?')) {
      serviceItems.split('?')
      Taro.navigateTo({ url: concatUrl('/pages/webview/index/index', { path: serviceItems.split('?')[0], rest: serviceItems.split('?')[1] }) })
    } else {
      Taro.navigateTo({ url: concatUrl('/pages/webview/index/index', { path: serviceItems }) })
    }
  }
  if (contentType == 5) {
    Taro.navigateTo({ url: concatUrl('/pages/goods/list/index', { id: serviceItems }) })
  }
  if (contentType == 6) {
    if (appId == 'wxc3f5e20f0716b841') {
      Taro.openEmbeddedMiniProgram({
        appId,
        path: serviceItems
      })
    } else {
      Taro.navigateToMiniProgram({
        appId,
        path: serviceItems
      })
    }
  }
}
